package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.entity.ShopDelivery;
import com.caocaog.market.entity.ShopDeliveryHis;
import com.caocaog.market.entity.ShopKilometerPrice;
import com.caocaog.market.mapper.ShopDeliveryHisMapper;
import com.caocaog.market.service.ShopDeliveryHisService;
import com.caocaog.market.service.ShopKilometerPriceService;
import com.caocaog.market.vo.ShopDeliveryVO;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.stream.Collectors;

/**
 * 店铺配送管理历史
 *
 * @author chechen
 * @date 2021-11-30
 */
@Service
@Transactional
@RequiredArgsConstructor
public class ShopDeliveryHisServiceImpl extends ServiceImpl<ShopDeliveryHisMapper, ShopDeliveryHis> implements ShopDeliveryHisService {

    private final ShopKilometerPriceService kilometerPriceService;

    @Override
    public ShopDeliveryVO queryById(Long id) {
        ShopDeliveryHis shopDeliveryHis = super.baseMapper.selectById(id);
        if (shopDeliveryHis == null)
            throw new JeecgBootException("未找到相应店铺配送管理历史, id-> " + id);
        return this.handleDeliveryHisToVO(shopDeliveryHis);
    }

    @Override
    public ShopDeliveryVO queryByShopId(String shopId) {
        // 查找最新的
        ShopDeliveryHis shopDeliveryHis = super.baseMapper.selectOne(new LambdaQueryWrapper<ShopDeliveryHis>()
                .eq(ShopDeliveryHis::getId, shopId).orderByDesc(ShopDeliveryHis::getId));
        if (shopDeliveryHis == null)
            throw new JeecgBootException("未找到相应店铺配送管理历史, shopId-> " + shopId);
        return this.handleDeliveryHisToVO(shopDeliveryHis);
    }

    @Override
    public ShopDeliveryVO queryByDeliveryId(Long deliveryId) {
        // 查找最新的
        ShopDeliveryHis shopDeliveryHis = super.baseMapper.selectOne(new LambdaQueryWrapper<ShopDeliveryHis>()
                .eq(ShopDeliveryHis::getShopId, deliveryId).orderByDesc(ShopDeliveryHis::getId));
        if (shopDeliveryHis == null)
            throw new JeecgBootException("未找到相应店铺配送管理历史, deliveryId-> " + deliveryId);
        return this.handleDeliveryHisToVO(shopDeliveryHis);
    }

    private ShopDeliveryVO handleDeliveryHisToVO(ShopDeliveryHis shopDeliveryHis) {
        ShopDeliveryVO shopDeliveryVO = new ShopDeliveryVO();
        shopDeliveryVO.setShopDelivery(new ShopDelivery(shopDeliveryHis));
        if (shopDeliveryHis.getIntervalRange() != null && shopDeliveryHis.getIntervalRange().length() > 0)
            shopDeliveryVO.setInterval(Arrays.stream(shopDeliveryHis.getIntervalRange().split(","))
                    .map(this.kilometerPriceService::queryShopKilometerPriceById)
                    .sorted(Comparator.comparing(ShopKilometerPrice::getKilometerStr))
                    .collect(Collectors.toList()));
        else shopDeliveryVO.setInterval(Collections.emptyList());
        return shopDeliveryVO;
    }

    @Override
    public String getCurrentLatestHisId(String deliveryId) {
        ShopDeliveryHis shopDeliveryHis = super.baseMapper.selectOne(new LambdaQueryWrapper<ShopDeliveryHis>()
                .eq(ShopDeliveryHis::getShopId, deliveryId).orderByDesc(ShopDeliveryHis::getId));
        if (shopDeliveryHis == null)
            throw new JeecgBootException("未找到相应店铺配送管理历史, deliveryId-> " + deliveryId);
        return shopDeliveryHis.getId();
    }

    @Override
    public String addShopDeliveryHis(ShopDeliveryHis shopDeliveryHis) {
        shopDeliveryHis.setLockVersion(1);
        super.baseMapper.insert(shopDeliveryHis);
        if (shopDeliveryHis.getId() == null)
            throw new JeecgBootException("新增店铺配送管理历史失败");
        return shopDeliveryHis.getId();
    }

}
